import 'dart:async';

import 'package:flutter/services.dart';
import 'package:tx_plugin/qq_response.dart';
import 'package:tx_plugin/qq_sdk_params.dart';
import 'package:tx_plugin/wechat_response.dart';
import 'package:tx_plugin/wx_sdk_params.dart';

/// 微信相关回调==

/// Response from share
StreamController<WeChatShareResponse> wxResponseShareController = new StreamController.broadcast();
Stream<WeChatShareResponse> get wxResponseFromShare => wxResponseShareController.stream;

/// Response from auth
StreamController<WeChatAuthResponse> wxResponseAuthController = new StreamController.broadcast();
Stream<WeChatAuthResponse> get wxResponseFromAuth => wxResponseAuthController.stream;

///Response from payment
StreamController<WeChatPaymentResponse> wxResponsePaymentController = new StreamController.broadcast();
Stream<WeChatPaymentResponse> get wxResponseFromPayment => wxResponsePaymentController.stream;

///Response from launching mini-programR
Stream<WeChatLaunchMiniProgramResponse> get wxResponseFromLaunchMiniProgram => wxResponseLaunchMiniProgramController.stream;
StreamController<WeChatLaunchMiniProgramResponse> wxResponseLaunchMiniProgramController = new StreamController.broadcast();

///Response from h5WxLaunchApp
StreamController<String?> h5WxLaunchAppController = new StreamController.broadcast();
Stream<String?> get h5WxLaunchApp => h5WxLaunchAppController.stream;

/// QQ相关回调
/// Response from share
StreamController<QQShareResponse> qqResponseShareController = new StreamController.broadcast();
Stream<QQShareResponse> get qqResponseFromShare => qqResponseShareController.stream;

/// Response from auth
StreamController<QQAuthResponse> qqResponseAuthController = new StreamController.broadcast();
Stream<QQAuthResponse> get qqResponseFromAuth => qqResponseAuthController.stream;

/// Response from getUserInfo
StreamController<QQGetUserInfoResponse> qqResponseGetUserInfoController = new StreamController.broadcast();
Stream<QQGetUserInfoResponse> get qqResponseFromGetUserInfo => qqResponseGetUserInfoController.stream;

/// Response from getUnionid
StreamController<String> qqResponseGetUnionidController = new StreamController.broadcast();
Stream<String> get qqResponseFromGetUnionid => qqResponseGetUnionidController.stream;

class TxPlugin {
  // 回调 - 原生回调flutter传回数据
  static Future<dynamic> _handler(MethodCall methodCall) {
    // 微信相关回调
    if ("onWxShareResponse" == methodCall.method) {
      // 微信分享回调
      wxResponseShareController.sink.add(WeChatShareResponse.fromMap(methodCall.arguments));
      print("微信分享回调---WeChatShareResponse");
    } else if ("onWxAuthResponse" == methodCall.method) {
      // 微信登录回调
      wxResponseAuthController.sink.add(WeChatAuthResponse.fromMap(methodCall.arguments));
      print("微信登录回调---WeChatAuthResponse");
    } else if ("onWxLaunchMiniProgramResponse" == methodCall.method) {
      // 微信小程序回调
      wxResponseLaunchMiniProgramController.sink.add(WeChatLaunchMiniProgramResponse.fromMap(methodCall.arguments));
      print("微信小程序回调---WeChatLaunchMiniProgramResponse");
    } else if ("onWxPayResponse" == methodCall.method) {
      // 微信支付回调
      wxResponsePaymentController.sink.add(WeChatPaymentResponse.fromMap(methodCall.arguments));
      print("微信支付回调---WeChatPaymentResponse");
    } else if ("h5WxLaunchApp" == methodCall.method) {
      // 微信浏览器h5唤起app
      h5WxLaunchAppController.sink.add(methodCall.arguments);
    }

    // QQ相关回调
    else if ("onQQShareResponse" == methodCall.method) {
      // QQ分享回调
      qqResponseShareController.sink.add(QQShareResponse.fromMap(methodCall.arguments));
      print('QQ分享回调---QQShareResponse');
    } else if ("onQQAuthResponse" == methodCall.method) {
      // QQ授权回调
      qqResponseAuthController.sink.add(QQAuthResponse.fromMap(methodCall.arguments));
      print("QQ授权回调---QQAuthResponse");
    } else if ("onQQGetUserInfoResponse" == methodCall.method) {
      // QQ获取用户信息回调
      qqResponseGetUserInfoController.sink.add(QQGetUserInfoResponse.fromMap(methodCall.arguments));
      print('QQ获取用户信息回调---QQGetUserInfoResponse');
    } else if ("onQQGetUnionid" == methodCall.method) {
      // QQ获取unionid - 用于安卓
      qqResponseGetUnionidController.sink.add(methodCall.arguments);
      print('QQ获取unionid回调---onQQGetUnionid');
    }

    return Future.value(true);
  }

  static MethodChannel _channel = const MethodChannel('tencent_plugin')..setMethodCallHandler(_handler);

  ///微信注册
  static const String REGISTER_WX = "register_wx";

  ///是否安装微信
  static const String IS_INSTALLED_WX = "is_installed_wx";

  ///微信授权
  static const String AUTHORIZATION_WX = "authorization_wx";

  ///微信分享文本
  static const String SHARE_TEXT_WX = "share_text_wx";
  static const String SHARE_IMAGE_WX = "share_image_wx";
  static const String SHARE_MUSIC_WX = "share_music_wx";
  static const String SHARE_VIDEO_WX = "share_video_wx";
  static const String SHARE_WEB_PAGE_WX = "share_web_page_wx";
  static const String SHARE_MINI_PROGRAM_WX = "share_mini_program_wx";

  ///微信支付
  static const String PAY_WX = "pay_wx";

  /// QQ是否授权获取设备信息
  static const String QQ_IS_AUTH_GET_DEVICE_INFO = "qq_is_auth_get_device_info";

  ///QQ注册
  static const QQ_REGISTER = "register_qq";

  ///QQ授权
  static const AUTHORIZATION_QQ = "authorization_QQ";

  ///QQ获取unionID
  static const UNIONID_QQ = "unionid_QQ";

  ///是否安装QQ
  static const IS_INSTALLED_QQ = "is_installed_qq";

  ///退出QQ登录
  static const LOGOUT_QQ = "logout_qq";

  ///获取用户信息
  static const GET_USER_INFO_QQ = "get_user_info_qq";

  ///分享文本到QQ
  static const SHARE_TEXT_QQ = "share_text_qq";

  ///分享图片到QQ
  static const SHARE_IMAGE_QQ = "share_image_qq";

  ///分享音乐到QQ
  static const SHARE_MUSIC_QQ = "share_music_qq";

  ///分享网页到QQ
  static const SHARE_WEB_PAGE_QQ = "share_web_page_qq";

  ///注册微信
  ///args String appId
  static Future<bool?> registerToWx(WXSdkParamsMap args) async {
    print("调用插件中的方法---registerToWx");
    print("调用的参数: ${args.toString()}");
    bool? isSuccess = await _channel.invokeMethod(REGISTER_WX, args.toMap());
    print("get result regiseterToWX is state $isSuccess");
    return isSuccess;
  }

  ///是否安装微信
  static Future<bool?> isInstalledWx(WXSdkParamsMap args) async {
    print("调用插件中的方法---isInstalledWx");
    bool? isSuccess = await _channel.invokeMethod(IS_INSTALLED_WX, args.toMap());
    print('isSuccess: $isSuccess');
    return isSuccess;
  }

  ///微信授权
  ///args String scope  "snsapi_userinfo"
  static Future<bool?> authorizationByWx(WXSdkParamsMap args) async {
    print("调用插件中的方法---authorizationByWx");
    return await _channel.invokeMethod(AUTHORIZATION_WX, args.toMap());
  }

  ///分享到微信的文本
  ///args String text
  ///args String targetScene
  static Future<bool?> shareTextToWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---shareTextToWX");
    return await _channel.invokeMethod(SHARE_TEXT_WX, args.toMap());
  }

  ///分享到微信的图片
  ///args String imageUrl
  ///args String targetScene
  static void shareImageToWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---shareImageToWX");
    await _channel.invokeMethod(SHARE_IMAGE_WX, args.toMap());
  }

  ///分享到微信的音乐
  ///args String musicUrl
  ///args String musicTitle
  ///args String musicDescription
  ///args String targetScene
  static void shareMusicToWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---shareMusicToWX");
    await _channel.invokeMethod(SHARE_MUSIC_WX, args.toMap());
  }

  ///分享到微信的视频
  ///args String videoUrl
  ///args String videoTitle
  ///args String videoDescription
  ///args String targetScene
  static void shareVideoToWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---shareVideoToWX");
    await _channel.invokeMethod(SHARE_VIDEO_WX, args.toMap());
  }

  ///分享到微信的网页
  ///args String webPageUrl
  ///args String webPageTitle
  ///args String webPageDescription
  ///args String targetScene
  static void shareWebPageToWX(WXSdkParamsMap args) async {
    print('分享微信的参数: $args');
    print("调用插件中的方法---shareWebPageToWX");
    await _channel.invokeMethod(SHARE_WEB_PAGE_WX, args.toMap());
  }

  ///分享到微信的小程序---只支持会话(好友)
  ///args String miniProgramWebPageUrl
  ///args String miniProgramWebPageTitle
  ///args String miniProgramWebPageDescription
  ///args String miniProgramId
  ///args String miniProgramPath
  ///args String miniProgramHdImageData
  static void shareMiniProgramToWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---shareMiniProgramToWX");
    await _channel.invokeMethod(SHARE_MINI_PROGRAM_WX, args.toMap());
  }

  ///微信支付
  ///args String wxAppId
  ///args String partnerId
  ///args String prepayId
  ///args String nonceStr
  ///args String timeStamp
  ///args String packageValue
  ///args String sign
  static void payByWX(WXSdkParamsMap args) async {
    print("调用插件中的方法---payByWX");
    await _channel.invokeMethod(PAY_WX, args.toMap());
  }

  // QQ是否授权获取设备信息
  static Future<bool?> qqIsAuthGetDeviceInfo() async {
    print("调用插件中的方法---qqIsAuthGetDeviceInfo");
    bool? isSuccess = await _channel.invokeMethod(QQ_IS_AUTH_GET_DEVICE_INFO);
    print('qqIsAuthGetDeviceInfo---state:$isSuccess');
    return isSuccess;
  }

  // QQ注册
  static Future<bool?> registerToQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---registerToQQ");
    bool? isSuccess = await _channel.invokeMethod(QQ_REGISTER, args.toMap());
    print('registerToQQ---state:$isSuccess');
    return isSuccess;
  }

  //QQ授权
  static Future<bool?> authorizationByQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---authorizationByQQ");
    bool? isSuccess = await _channel.invokeMethod(AUTHORIZATION_QQ, args.toMap());
    print('authorizationByQQ---state:$isSuccess');
    return isSuccess;
  }

  //QQ获取unionID
  static Future<bool> getUnionidByQQ() async {
    print("调用插件中的方法---UNIONID_QQ");
    bool isSuccess = await _channel.invokeMethod(UNIONID_QQ);
    print('getUnionidByQQ---state:$isSuccess');
    return isSuccess;
  }

  //是否安装QQ
  static Future<bool?> isInstalledByQQ() async {
    print("调用插件中的方法---isInstalledByQQ");
    bool? isSuccess = await _channel.invokeMethod(IS_INSTALLED_QQ);
    print('isInstalledByQQ---state:$isSuccess');
    return isSuccess;
  }

  //退出QQ
  static Future<void> qqLogout(QQSDKParamMap args) async {
    print("调用插件中的方法---qqLogout");
    await _channel.invokeMethod(LOGOUT_QQ, args.toMap());
  }

  ///分享文本到QQ
  ///String targetScene QQ好友(Friend)/QQ空间
  ///String title 标题
  ///String summary 摘要
  ///String targetUrl 连接的地址
  ///String imageUrl 图片的链接
  ///String appName App名称
  static void shareTextToQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---shareTextToQQ");
    await _channel.invokeMethod(SHARE_TEXT_QQ, args.toMap());
  }

  ///分享图片到QQ---只支持QQ
  ///String targetScene QQ好友(Friend)/QQ空间
  ///String imageUrl 图片的链接
  ///String appName App名称
  static void shareImageToQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---shareImageToQQ");
    await _channel.invokeMethod(SHARE_IMAGE_QQ, args.toMap());
  }

  ///分享音乐到QQ---支持QQ
  ///String targetScene QQ好友(Friend)/QQ空间
  ///String title 标题
  ///String summary 摘要
  ///String targetUrl 连接的地址
  ///String imageUrl 图片的链接
  ///String appName App名称
  static void shareMusicToQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---shareMusicToQQ");
    await _channel.invokeMethod(SHARE_MUSIC_QQ, args.toMap());
  }

  ///分享网页到QQ---支持QQ/QZone
  ///String targetScene QQ好友(Friend)/QQ空间
  ///String title 标题
  ///String summary 摘要
  ///String targetUrl 连接的地址
  ///String imageUrl 图片的链接
  ///String appName App名称
  static void shareWebPageToQQ(QQSDKParamMap args) async {
    print("调用插件中的方法---shareWebPageToQQ");
    await _channel.invokeMethod(SHARE_WEB_PAGE_QQ, args.toMap());
  }

  static const String platformVersionKey = "getPlatformVersion";

  static Future<String?> get platformVersion async {
    final String? version = await _channel.invokeMethod(platformVersionKey);
    return version;
  }
}
